package com.example.server.service.admin.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.common.constant.Result;
import com.example.server.entity.Sword;
import com.example.server.entity.Version;
import com.example.server.mapper.VersionMapper;
import com.example.server.service.admin.VersionService;
import com.example.server.vo.VersionVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * (Version)表服务实现类
 *
 * @author china.fuyao@outlook.com
 * @since 2020-04-20 18:39:47
 */
@Service("versionService")
public class VersionServiceImpl extends ServiceImpl<VersionMapper, Version> implements VersionService {
    /**
     * 版本列表
     *
     * @param current
     * @param size
     * @param clientType
     * @param clientSys
     * @param customerId
     * @return
     */
    @Override
    public Result versionList(Integer current, Integer size, Integer clientType, Integer clientSys, Integer customerId) {
        IPage iPage = new Page(current, size);
        QueryWrapper<Version> wrapper = new QueryWrapper<Version>();
        wrapper.orderByDesc("add_time");
        IPage<Version> data;
        if (null == clientType && null == clientSys) {
            data = page(iPage, wrapper);
        } else {
            if (null != clientType) {
                wrapper.eq("client_type", clientType);
            }
            if (null != clientSys) {
                wrapper.eq("sys_type", clientSys);
            }
            data = page(iPage, wrapper);
        }
        return new Result(data);
    }

    /**
     * 新增版本
     *
     * @param versionVO
     * @param customerId
     * @return
     */
    @Override
    public Result addVersion(VersionVO versionVO, Integer customerId) {
        Version version = new Version();
        version.setVersionNum(versionVO.getVersionNum());
        version.setUrl(versionVO.getUrl());
        version.setContent(versionVO.getContent());
        version.setClientType(versionVO.getSysClient());
        version.setSysType(versionVO.getSysType());
        version.setIsForce(versionVO.getForce());
        version.setAddTime(LocalDateTime.now());
        boolean save = save(version);
        if (save) {
            return Result.OK();
        }
        return Result.Error();
    }

    /**
     * 更新版本
     *
     * @param versionVO
     * @param customerId
     * @return
     */
    @Override
    public Result updateVersion(VersionVO versionVO, Integer customerId) {
        Version version = getById(versionVO.getVersionId());
        if (null != version) {
            version.setVersionNum(versionVO.getVersionNum());
            version.setUrl(versionVO.getUrl());
            version.setContent(versionVO.getContent());
            version.setClientType(versionVO.getSysClient());
            version.setSysType(versionVO.getSysType());
            version.setIsForce(versionVO.getForce());
            version.setAddTime(LocalDateTime.now());
            boolean save = save(version);
            if (save) {
                return Result.OK();
            }
            return Result.Error();
        }
        return Result.Error("版本不存在");
    }

    /**
     * 删除版本
     *
     * @param versionIds
     * @param customerId
     * @return
     */
    @Override
    public Result deleteVersion(String versionIds, Integer customerId) {
        if (StrUtil.isNullOrUndefined(versionIds)) {
            return Result.Error("请求参数错误");
        }
        String[] split = versionIds.split(",");
        List<String> asList = Arrays.asList(split);
        boolean remove = removeByIds(asList);
        if (remove) {
            return Result.OK();
        }
        return Result.Error();
    }
}